<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')"
             align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="单据日期" prop="other_date">
                        <el-date-picker v-model="form.other_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="单据编号" prop="other_code">
                        <el-input v-model="form.other_code" placeholder="请输入单据编号" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="选择付款单位">
                    <el-radio-group v-model="unit" class="ml-4" @change="radioChange">
                        <el-radio :label="1" size="large">客户</el-radio>
                        <el-radio :label="2" size="large">供应商</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="付款单位">
                        <el-select v-model="form.customer_id" filterable clearable placeholder="请选择客户" style="width:100%" v-if="unit == 1">
                            <el-option v-for="item in clientList" :key="item.id" :label="item.customer_name" :value="item.id"/>
                        </el-select>
                        <el-select v-model="form.supplier_id" filterable clearable placeholder="请选择供应商" style="width:100%" v-if="unit == 2">
                            <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="收入类型" prop="type">
                        <el-select v-model="form.type" filterable clearable placeholder="请选择" style="width:100%">
                            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="结算账户" prop="settlement_id">
                        <el-select v-model="form.settlement_id" filterable clearable placeholder="请选择账户" style="width:100%">
                            <el-option v-for="item in accountList" :key="item.id" :label="item.account" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="金额" prop="money">
                        <el-input v-model="form.money" placeholder="请输入金额" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
            </el-form-item>
            <el-form-item label="附件" prop="enclosure">
                <sc-upload-file v-model="form.enclosure" :limit="1">
                    <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                </sc-upload-file>
            </el-form-item>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增其他收入',
				edit: '编辑其他收入',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                other_code:"",
                other_date:"",
                customer_id:"",
                supplier_id:"",
                type:"",
                settlement_id:"",
                money:"",
                enclosure:"",
                remark:"",
                other_type: 1,
			},
			//验证规则
			rules: {
				account: [{ required: true, message: '请输入账号名称' }],
			},
            accountList: [],
            typeList: [],
            clientList: [],
            applyList: [],
            unit: 1
		}
	},
	mounted() {
        this.getAccount()
        this.getType()
        this.getClient()
        this.getApply()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.finance.sz.edit.post(this.form);
					} else {
						res = await this.$API.finance.sz.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            this.form.other_code = data.other_code
            this.form.other_date = data.other_date
            this.unit = data.customer ? 1 : 2
            this.form.customer_id = data.customer_id
            this.form.supplier_id = data.supplier_id
            this.form.type = data.type.id
            this.form.settlement_id = data.settlement_id
            this.form.money = data.money
            this.form.enclosure = data.enclosure
            this.form.remark = data.remark
		},
        // 账号
        async getAccount(){
            var res = await this.$API.finance.account.all.get();
            this.accountList = res.data;
        },
        // 收入
        async getType(){
            var res = await this.$API.finance.szcate.all.get({type:1});
            this.typeList = res.data;
        },
        // 客户
        async getClient(){
            var res = await this.$API.client.client.all.get();
            this.clientList = res.data;
        },
        // 供应商
        async getApply(){
            var res = await this.$API.apply.apply.all.get();
            this.applyList = res.data
        },
        radioChange(){
            this.form.customer_id = ""
            this.form.supplier_id = ""
        },
	}
}
</script>

<style></style>
